package base.demo;

import base.utils.PrintUtils;

import java.util.Arrays;

public class BubbleSortDemo {

    static int[]arrays={2,3,1,5,4};

    public static void main(String[] args) {
        bubbleSort3(arrays);
    }

    public static void bubbleSort(int[] array){

        int arrayLen=array.length;
        int[]newArray= Arrays.copyOf(array,arrayLen);

        for (int i=1;i<arrayLen;i++){

//            System.out.print(i+"  ;   ");
            for (int j=0;j<arrayLen-i;j++){
                System.out.print(j+"  ,");
                if (newArray[j]>newArray[j+1]){
                    int sub=newArray[j];
                    newArray[j]=newArray[j+1];
                    newArray[j+1]=sub;
                }
            }
            System.out.println();
        }


        for (int item:newArray){
            System.out.print(item  +"  ");
        }

    }

  public static void bubbleSort2(int[] array){

        int len=array.length;
        int[]newArray= Arrays.copyOf(array,len);
        for (int i=0;i<len;i++){
            for (int j=1;j<len-i;j++){
                if (newArray[j-1]>newArray[j]){
                    int sub=newArray[j];
                    newArray[j]=newArray[j-1];
                    newArray[j-1]=sub;
                }
            }
        }

      PrintUtils.print(newArray);

    }


    public static void bubbleSort3(int[] array){

        int len=array.length;
        int[]newArray= Arrays.copyOf(array,len);
        for (int i=1;i<len;i++){
            for (int j=len-i;j>0;j--){
                if (newArray[j-1]>newArray[j]){
                    int sub=newArray[j];
                    newArray[j]=newArray[j-1];
                    newArray[j-1]=sub;
                }
            }
        }

      PrintUtils.print(newArray);

    }





    /*
    * length=5
    *
    * i=1   j=  0  1  2  3
    * i=2   j=  0  1  2
    * i=3   j=  0  1
    * i=4   j=  0
    *
    * */



}
